using System;
using System.Web;
using newtelligence.DasBlog.Runtime;
using newtelligence.DasBlog.Web.Core;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Net;
using System.IO;
using System.Web.Security;

namespace newtelligence.DasBlog.Web.Services
{
	/// <summary>
	/// Summary description for MicrosummaryHandler.
	/// </summary>
    public partial class WebWebServices
	{
		[OperationContract, WebGet(UriTemplate="/microsummary")]
		public Stream MicrosummaryHandler( )
		{
			try
			{
				string languageFilter = WebOperationContext.Current.IncomingRequest.Headers[HttpRequestHeader.AcceptLanguage];
				if ( !Roles.IsUserInRole("admin") )
				{
					languageFilter = "";
				}

				EntryCollection entries =  dataService.GetEntriesForDay( DateTime.UtcNow, siteConfig.GetConfiguredTimeZone(), languageFilter,1,1,String.Empty );
                MemoryStream memStream = new MemoryStream();
                StreamWriter stw = new StreamWriter(memStream);

				if(entries != null && entries.Count > 0)
				{
					Entry e = entries[0];
					stw.WriteLine(e.Title);
				}
                stw.Flush();
                memStream.Position = 0;
                return memStream;
			}
			catch (Exception ex)
			{
				loggingService.AddEvent(new EventDataItem(EventCodes.Error,"Error generating Microsummary: " + ex.ToString(),String.Empty));
                return null;
			}
		}
	}
}
